{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python是强类型的动态脚本语言"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 强类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**强类型：** 不允许不同类型相加。 例如：整型+字符串会报类型错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T14:50:28.610299Z",
     "start_time": "2023-07-21T14:50:28.368144Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 整型 + 字符串：报错\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;241;43m50\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpython\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n",
      "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "# 整型 + 字符串 -- 报错\n",
    "50 + \"python\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T14:50:58.353580Z",
     "start_time": "2023-07-21T14:50:58.343369Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'50python'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# str(): 整型 -> 字符串\n",
    "str(50) + \"python\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T14:51:00.597709Z",
     "start_time": "2023-07-21T14:51:00.593854Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# int(): 字符串 -> 整型\n",
    "50 + int('5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 动态"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**动态：**\n",
    "- 不使用显示数据类型来进行声明\n",
    "- 且确定一个变量的类型是在给它赋值的时候"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T14:52:11.073431Z",
     "start_time": "2023-07-21T14:52:11.069800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不使用显示数据类型来进行声明\n",
    "uid = 100\n",
    "type(uid) # 查看变量类型\n",
    "uid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T14:52:13.248344Z",
     "start_time": "2023-07-21T14:52:13.244689Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给变量赋值时，自动确定变量的类型\n",
    "uid = \"一百\"\n",
    "type(uid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 脚本语言"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**脚本语言：**\n",
    "- 一般是解释性语言，运行代码只需要一个解释器，不需要编译器\n",
    "- C 和 Java 则需要编译"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量\n",
    "## 对象和引用\n",
    "Python 中，一切皆对象。\n",
    "\n",
    "每个对象由：标识（identity）、类型（type）、value（值）组 成。\n",
    "\n",
    "对象的本质就是：一个内存块，拥有特定的值，支持特定类型的相关操作。 \n",
    "- 1) 标识用于唯一标识对象，通常对应于对象在计算机内存中的地址。使用内置函数 id(obj)可返回对象 obj 的标识。 \n",
    "- 2) 类型用于表示对象存储的“数据”的类型。类型可以限制对象的取值范围以及可执 行的操作。可以使用 type(obj)获得对象的所属类型。 \n",
    "- 3) 值表示对象所存储的数据的信息。使用 print(obj)可以直接打印出值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T15:00:52.490021Z",
     "start_time": "2023-07-21T15:00:52.486421Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4367819568"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标识、地址\n",
    "id(uid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T15:00:54.902864Z",
     "start_time": "2023-07-21T15:00:54.898832Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据类型\n",
    "type(uid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T15:00:57.422823Z",
     "start_time": "2023-07-21T15:00:57.419945Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一百\n"
     ]
    }
   ],
   "source": [
    "# 值\n",
    "print(uid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "变量位于：栈内存\n",
    "\n",
    "对象位于：堆内存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](images/1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量的声明与赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "变量（variable）的主要作用就是 为 Python 中的某个值起个 名字。类似于“张三” “李 四” “王五”一样的人名，便于记忆。\n",
    "\n",
    "在 Python 语言中，声明变量的同时需要为其赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T22:56:03.683845Z",
     "start_time": "2023-07-21T22:56:03.667807Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'my_name' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [9]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 声明变量 my_name 没有赋值 \u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mmy_name\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'my_name' is not defined"
     ]
    }
   ],
   "source": [
    "# 声明变量 my_name 没有赋值 \n",
    "my_name "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-21T22:56:38.051860Z",
     "start_time": "2023-07-21T22:56:38.049261Z"
    }
   },
   "outputs": [],
   "source": [
    "# 声明整数类型变量\n",
    "a = 36\n",
    "\n",
    "# 声明字符串类型变量\n",
    "b = 'abc'\n",
    "\n",
    "# 声明浮点类型变量\n",
    "c = 55.41"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
